﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PhysicsControlModule.Models;

namespace PhysicsControlModule.Daos
{
    public class PeriphParamDao
    {
        public static List<PeriphParam> Load(int labworkId)
        {
            using (CmDbEntities db = new CmDbEntities())
                return (
                    from pp in db.PeriphParam 
                    from rlgp in db.RelLabworkGroupPeriphParam
                    where rlgp.LabworkId == labworkId 
                    where rlgp.ParameterId == pp.Id
                    select pp).ToList();
        }

        public static int GetCount(int labworkId, int studentId)
        {
            // TODO: Write query
            using (CmDbEntities db = new CmDbEntities())
                return (
                    from pp in db.PeriphParam

                    from rlpu in db.RelLabworkPeriphParamUser
                    where rlpu.LabworkId == labworkId
                    where rlpu.UserId == studentId
                    where rlpu.ParameterId == pp.Id

                    select rlpu).Count();
        }

        public static int GetRightCount(int labworkId, int studentId)
        {
            // TODO: Write query
            using (CmDbEntities db = new CmDbEntities())
                return (
                    from pp in db.PeriphParam

                    from rlpu in db.RelLabworkPeriphParamUser
                    where rlpu.LabworkId == labworkId
                    where rlpu.UserId == studentId
                    where rlpu.ParameterId == pp.Id

                    from rlgp in db.RelLabworkGroupPeriphParam
                    where rlgp.ParameterId == pp.Id
                    where rlgp.LimitMin <= rlpu.Value
                    where rlgp.LimitMax >= rlpu.Value

                    select pp).Count();
        }
    }
}